<<<<<<< HEAD
﻿package com.example.demo.controller;
=======
package com.example.demo.controller;
>>>>>>> c827499e8bb31ca32bc4e6604c6b0e46aefc968e

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsCategory;
import com.example.demo.common.Response;
import com.example.demo.service.CmsCategoryService;

import io.swagger.annotations.Api;
<<<<<<< HEAD
import io.swagger.annotations.ApiImplicitParam;
=======
>>>>>>> c827499e8bb31ca32bc4e6604c6b0e46aefc968e
import io.swagger.annotations.ApiOperation;

@Api(tags = "栏目信息管理服务")
@RestController
@RequestMapping("/category")
public class CmsCategoryController {
<<<<<<< HEAD
	
	@Autowired(required = false)
	private CmsCategoryService categoryService;
	
	@ApiOperation(value = "按照栏目编号，删除栏目信息！")
	@DeleteMapping("/deleteById")
	public Response<String> deleteById(String id){
		//调用业务方法
		int num = categoryService.deleteById(id);
		//根据处理结果，返回不同的数据。
		if(num==-1) {
			return Response.error("请先删除所有子栏目!");
		}
		if(num==-2) {
			return Response.error("请先删除该栏目下所有文章！");
		}
		if(num==0) {
			return Response.error("此栏目不存在！");
		}
		return Response.ok("成功删除！");
	}
	
	@ApiOperation(value = "批量删除栏目信息！")
	@DeleteMapping("/batchDeleteByIds")
	public Response<String> deleteByIds(String[] ids){
		String result = 
				categoryService.batchDelete(ids);
		if(result.startsWith("success")) {
			return Response.ok(result);
		}else {
			return Response.error(result);
		}
	}
	
	
	@ApiOperation(value = "保存或者更新栏目信息", 
			notes = "如果传递参数时指明了栏目编号，那么执行更新操作<br/>"
					+ "如果传递参数时没有指明栏目编号，那么执行插入操作！")
	@PostMapping("/saveOrUpdateCategory")
	public Response<String> saveOrUpdate(CmsCategory category){
		boolean b = categoryService.saveOrUpdateCmsCategory(category);
		if(b) return Response.ok("操作成功！");
		else  return Response.error("操作失败！");
	}
	
	
	@ApiOperation(value = "获取所有栏目信息",notes = "从数据库检索所有栏目信息返回")
	@GetMapping("/all")
	public Response<List<CmsCategory>> queryAll(){
		//调用业务方法，进行数据处理。再将处理结果返回。
		List<CmsCategory> cs = categoryService.getAllCategory();
		
		return Response.ok(cs);
=======

	@Autowired
	CmsCategoryService categoryService;
	
	@RequestMapping("/insert")
	public Response<String> insertData(CmsCategory category){
		categoryService.insert(category);
		return Response.ok("success");
	}
	
	@DeleteMapping("/deleteById")
	public Response<String> deleteByIds(String id){
		int count = categoryService.deleteById(id);
		return Response.ok("删除成功"+count+"条栏目!");
	}
	
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdate(CmsCategory category){
		categoryService.saveOrUpdate(category);
		return Response.ok("操作成功!");
	}
	
	@ApiOperation(value="获取所有栏目信息",notes="从数据库中检索所有栏目信息返回")
	@GetMapping("/All")
	public Response<List<CmsCategory>> findAll(){
		List<CmsCategory> all = categoryService.findAll();
		return Response.ok(all);
	}
	
	@RequestMapping("/select")
	public Response<CmsCategory> selectById(String id){
		CmsCategory data = categoryService.selectById(id);
		return Response.ok(data);
>>>>>>> c827499e8bb31ca32bc4e6604c6b0e46aefc968e
	}
}